package miggy.utils;

import miggy.cpu.DataSize;

/*
//  Miggy - Java Amiga Emulator
//  Copyright (c) 2008, Tony Headford
//  All rights reserved.
//
//  Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
//  following conditions are met:
//
//    o  Redistributions of source code must retain the above copyright notice, this list of conditions and the
//       following disclaimer.
//    o  Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
//       following disclaimer in the documentation and/or other materials provided with the distribution.
//    o  Neither the name of the Miggy Project nor the names of its contributors may be used to endorse or promote
//       products derived from this software without specific prior written permission.
//
//  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
//  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
//  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
//  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
//  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
//  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
//  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// $Revision: 2 $
*/
public class TextUtil
{
	static final char[] HEX = "0123456789ABCDEF".toCharArray();

	public static String toHex(byte b)
	{
		StringBuffer sb = new StringBuffer(4);
		sb.append(HEX[(b & 0xF0) >> 4]);
		sb.append(HEX[(b & 0x0F)]);
		return sb.toString();
	}

	public static String toHex(short s)
	{
		StringBuffer b = new StringBuffer(4);
		b.append(HEX[(s & 0xF000) >> 12]);
		b.append(HEX[(s & 0x0F00) >> 8]);
		b.append(HEX[(s & 0x00F0) >> 4]);
		b.append(HEX[(s & 0x000F)]);
		return b.toString();
	}

	public static String toHex(int i)
	{
		StringBuffer b = new StringBuffer(8);
		b.append(HEX[((i & 0xF0000000) >> 28) & 0x0F]);
		b.append(HEX[(i & 0x0F000000) >> 24]);
		b.append(HEX[(i & 0x00F00000) >> 20]);
		b.append(HEX[(i & 0x000F0000) >> 16]);
		b.append(HEX[(i & 0xF000) >> 12]);
		b.append(HEX[(i & 0x0F00) >> 8]);
		b.append(HEX[(i & 0x00F0) >> 4]);
		b.append(HEX[(i & 0x000F)]);
		return b.toString();
	}

	public static String toHex(int value, DataSize size)
	{
		switch(size)
		{
			case Byte:
				return toHex((byte)value);
			case Word:
				return toHex((short)value);
			case Long:
				return toHex(value);
		}

		throw new RuntimeException("Somehow size wasn't one of the enum values");
	}

	public static String toHexSigned(int value, DataSize size)
	{
		StringBuilder b = new StringBuilder(9);
		boolean neg = false;

		switch(size)
		{
			case Byte:
				neg = (value & 0x80) != 0;
				break;
			case Word:
				neg = (value & 0x8000) != 0;
				break;
			case Long:
				neg = (value & 0x80000000) != 0;
				break;
			default:
				throw new IllegalArgumentException("Unknown size: " + size);
		}

		if(neg)
		{
			value = ((~value) + 1) & size.mask();
			b.append('-');
		}

		switch(size)
		{
			case Byte:
			{
				b.append(toHex((byte)value));
				break;
			}
			case Word:
			{
				b.append(toHex((short)value));
				break;
			}
			case Long:
			{
				b.append(toHex(value));
				break;
			}
			default:
				throw new RuntimeException("Somehow size wasn't one of the enum values");
		}
		return b.toString();
	}



	public static String toHexParam(int value, DataSize size, boolean signed)
	{
		if(value == 0)	//quick out
			return "0";

		StringBuilder b = new StringBuilder(9);
		boolean neg = false;

		if(signed)
		{
			switch(size)
			{
				case Byte:
					neg = (value & 0x80) != 0;
					break;
				case Word:
					neg = (value & 0x8000) != 0;
					break;
				case Long:
					neg = (value & 0x80000000) != 0;
					break;
				default:
					throw new IllegalArgumentException("Unknown size: " + size);
			}

			if(neg)
			{
				value = ((~value) + 1) & size.mask();
				b.append('-');
			}
		}

		boolean started = false;
		int mask = 0xf0000000;
		int shift = 28;
		for(int n = 0; n < 8; n++)
		{
			if((value & mask) != 0)
			{
				started = true;
			}
			if(started)
			{
				b.append(HEX[((value & mask) >> shift) & 0x0F]);
			}
			shift -= 4;
			mask >>= 4;
		}
		return b.toString();
	}


	public static String pad(String s, int length)
	{
		StringBuilder sb = new StringBuilder(length);

		int count = length - s.length();
		if(count > 0)
		{
			sb.append(s);
			for(int i = 0; i < count; i++)
			{
				sb.append(' ');
			}
		}
		else
		{
			sb.append(s.substring(0, length - 1));
		}

		return sb.toString();
	}

	public static String decodeRegList(int list)
	{
		StringBuilder sb = new StringBuilder();
		int mask = 1;
		boolean started = false;
		int startNum = -1;
		char reg = 'd';

		for(int i = 0; i < 2; i++)
		{
			for(int n = 0; n < 8; n++)
			{
				if((list & mask) != 0)
				{
					if(!started)
					{
						if(startNum >= 0)
							sb.append('/');

						sb.append(reg);
						sb.append(n);
						started = true;
						startNum = n;
					}
				}
				else
				{
					if(started)
					{
						started = false;
						if(n > startNum + 1)
						{
							sb.append("-");
							sb.append(reg);
							sb.append(n - 1);
						}
					}
				}
				mask <<= 1;
			}
			reg = 'a';
			started = false;
		}
		return sb.toString();
	}
}
